package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ViewProductServlet", value = "/ViewProductServlet")
public class ViewProductServlet extends HttpServlet {
    private static final String VIEW_PRODUCT="/WEB-INF/jsp/catalog/Product.jsp";
    private String productId;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        productId=request.getParameter("productId");
        CatalogService service=new CatalogService();
        Product product= service.getProduct(productId);
        List<Item> itemList=service.getItemListByProduct(productId);

        HttpSession session=request.getSession();
        session.setAttribute("product",product);
        session.setAttribute("itemList",itemList);

        request.getRequestDispatcher(VIEW_PRODUCT).forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }
}
